package com.leonardo.beanme.metadatahandlers.tags;

import org.xml.sax.Attributes;

import com.leonardo.beanme.exceptions.XmlReadingException;
import com.leonardo.beanme.metadatahandlers.FieldMetadataContainer;
import com.leonardo.beanme.metadatahandlers.TagHandler;

public abstract class InnerFieldTagHandler extends TagHandler {

	protected String innerText;
	
	public void startTag(Attributes attributes) {
		FieldMetadataContainer ownerFieldInfo = owner.getFieldInfo();
		if(ownerFieldInfo != null && !isNullField(ownerFieldInfo)) {
			throw new XmlReadingException("Double tag " + getTagName() + " in bean " 
					+ owner.getBeanInfo().getId());
		} 
	}
	
	public void characters(char[] ch, int start, int lenght) {
		StringBuffer innerText = new StringBuffer(lenght); 
		/* Start DEBUG */
		System.out.print("Inside text: ");
		for(int i = start; i < start +  lenght; i++) {
			System.out.print(ch[i]);
			innerText.append(ch[i]);
		}
		System.out.println();
		/* End DEBUG */
		this.innerText = innerText.toString();
	}
	
	protected boolean isNullField(FieldMetadataContainer info) {
		return !info.constains(this.getClass());
	}
	
	public String getInnerText() {
		return innerText;
	}
	
	public void setInnerText(String innerText) {
		this.innerText = innerText;
	}
	
}
